package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import service.model.Club;
import service.model.Club2;
import service.model.School;
import service.model.Student;
import service.model.User;
import service.model.UserForm;

@Controller
@RequestMapping("/bindData")
public class BindDataController {
	
	/*@RequestMapping
	public void index()
	{
		// 转向测试主页
	}*/
	
	
	
	/**
	 *  绑定date类型的步骤
	 *  1 、   在 Date 类型的属性上加入 @DateTimeFormat 注解

		2、   加入 joda 相关的包
		
		3、   在 SpringMVC 配置文件中加入 <mvc:annotation-driven />
	 * @param user
	 */
	@RequestMapping("/bindDate")
	public void bindDate(User user)
	{
		System.out.println("=========date类型绑定=========");
		System.out.println(user);
	}
	
	@RequestMapping("/requestBodyString")
	@ResponseBody
	public String requestBodyString(@RequestBody String string,HttpServletRequest request){
		System.out.println(string);
		return string;
	}
	
	@RequestMapping("/requestBodyObject")
	@ResponseBody
	public Student requestBodyObject(@RequestBody Student student,HttpServletRequest request){
		System.out.println(student);
		return student;
	}
	
	@RequestMapping("/requestBodyList2String")
	@ResponseBody
	public String requestBodyList2String(@RequestBody String string,HttpServletRequest request){
		System.out.println(string);
		return string;
	}
	
	
	@RequestMapping("/requestBodyList2List")
	@ResponseBody
	public List<Student> requestBodyList2List(@RequestBody List<Student> students,HttpServletRequest request){
		System.out.println(students);
		return students;
	}
	
	@RequestMapping("/requestBodyListArray2List")
	@ResponseBody
	public List<List<Student>> requestBodyListArray2List(@RequestBody List<List<Student>> students,HttpServletRequest request){
		System.out.println(students);
		return students;	
	}
	
	
	@RequestMapping("/requestBodySchool2School")
	@ResponseBody
	public School requestBodySchool2Scholl(@RequestBody School school,HttpServletRequest request){
		System.out.println(school);
		return school;
	}
	
	
	@RequestMapping("/test1")
	public void test1(String name, Integer age, Double income, Boolean isMarried, String[] interests)
	{
		System.out.println("简单数据类型绑定=========");
		System.out.println("名字:" + name);
		System.out.println("年龄:" + age);
		System.out.println("收入:" + income);
		System.out.println("已结婚:" + isMarried);
		System.out.println("兴趣:");
		for (String interest : interests)
		{
			System.out.println(interest);
		}
		System.out.println("====================");
	}
	
	@RequestMapping("/test2")
	public void test2(User user)
	{
		System.out.println("简单对象类型绑定=========");
		System.out.println("名字:" + user.getName());
		System.out.println("年龄:" + user.getAge());
		System.out.println("收入:" + user.getIncome());
		System.out.println("已结婚:" + user.getIsMarried());
		System.out.println("========================");
	}
	
	@RequestMapping("/test3")
	public void test3(Club club)
	{
		System.out.println("list类型绑定=========");
		System.out.println("会员:");
		for (User user : club.getUsers())
		{
			System.out.println(user.getName());
		}
		System.out.println("========================");
	}
	
	@RequestMapping("/test4")
	public void test4(Club2 club)
	{
		System.out.println("set类型绑定=========");
		System.out.println("会员:");
		for (User user : club.getUsers())
		{
			System.out.println(user.getName());
		}
		System.out.println("========================");
	}
	
	@RequestMapping("/test5")
	public void test5(UserForm userForm)
	{
		System.out.println("Map类型绑定=========");
		System.out.println("名字:" + userForm.getUserMap().get("name"));
		System.out.println("年龄:" + userForm.getUserMap().get("age"));
		System.out.println("收入:" + userForm.getUserMap().get("income"));
		System.out.println("已结婚:" + userForm.getUserMap().get("isMarried"));
		System.out.println("========================");
	}
	
	@RequestMapping("/test6")
	public void test6(UserForm userForm)
	{
		System.out.println("复合类型绑定=========");
		System.out.println("名字:" + userForm.getUser().getName());
		System.out.println("年龄:" + userForm.getUser().getAge());
		System.out.println("收入:" + userForm.getUser().getIncome());
		System.out.println("已结婚:" + userForm.getUser().getIsMarried());
		System.out.println("========================");
	}
	
	
}
